/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.control;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rf.sys.model.DepUser;
import com.rf.sys.model.Department;
import com.rf.sys.model.Dictionary;
import com.rf.sys.service.DepUserService;
import com.rf.sys.service.DepartmentService;
import com.rf.sys.frm.BaseController;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.UuidUtil;

@Controller
@RequestMapping("/business/department")
public class DepartmentController extends BaseController{
	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(DepartmentController.class);
	private static final long serialVersionUID = -120161117110117883L;
	
	@Resource
	private DepartmentService departmentService;
	@Resource
	private DepUserService depUserService;
	
	/**
	 * 初始化页面信息
	 * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
	 */
	@RequestMapping(value = "index")
    public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/business/department/department_list");
		return modelAndView; 
    }
    
    /**
	 * 搜索页面信息
	 * @param Page,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "query")
    @ResponseBody
    public void query (PageUtils pageUtils, Department department, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	//获取分页、查询参数，进行数据封装
    	Map searchMap = departmentService.getDepartmentPage(department, pageUtils);
    	super.responsePrint(request, response, JsonUtils.mapToJson(searchMap));
    }
    
    /**
     * 编辑页面初始化
     * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "edit")
    public ModelAndView edit (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();
		String id = request.getParameter("id");
		Department department = new Department();
		if(id != null && !id.equals("")){
			department = departmentService.getDepartmentByID(id);	
		}else{
			department.setState(ConfigUtils.ENABLESTATE);
		}
		modelAndView.addObject("department", department);
		modelAndView.setViewName("/business/department/department_edit");
        return modelAndView; 
    }
    
    /**
     * 保存数据信息
     * @param Department,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "save")
    public void save (Department department, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	String ret = "success";
		if(department !=null && department.getId()!=null && !department.getId().equals("")){
			//修改
			departmentService.updateDepartment(department);
		}else{
			String id = UuidUtil.get32UUID();
			department.setId(id);
			//新增
			departmentService.addDepartment(department);
		}
		super.resPrint(request, response, ret);//成功 
    }
    
    /**
     * 删除数据信息
     * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "delete")
    @ResponseBody
    public void delete (HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String ids = request.getParameter("ids");
    	String a[] = ids.split(","); 
    	for(int i=0;i<a.length;i++){
    		departmentService.deleteDepartment(a[i]);
    	}
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 更新数据状态
     * @param department,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "updateState")
    @ResponseBody
    public void updateState (Department department, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	departmentService.updateDepartment(department);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 根据数据字典获取下拉
     * @param request
     * @param response
     * @param modelMap
     * @throws Exception 
     */
	@RequestMapping(value = "dicbykey", method = RequestMethod.GET)
	@ResponseBody
	public void queryDicbykey(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Department> rlist=new ArrayList<Department>();
		List<Department> list = departmentService.getDepartment();
		Department department=new Department();
		department.setId("");
		department.setName("");
		rlist.add(department);
		Department sysdic=new Department();
		sysdic.setId("");
		sysdic.setName("请选择...");
		rlist.add(sysdic);
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				rlist.add(list.get(i));
			}
		}
		JSONArray jsonarray = JSONArray.fromObject(rlist);  
		responsePrint(request,response, jsonarray.toString());
	}
	
	 /**
     * 根据数据字典获取下拉
     * @param request
     * @param response
     * @param modelMap
     * @throws Exception 
     */
	@RequestMapping(value = "departmentById", method = RequestMethod.GET)
	@ResponseBody
	public void queryValuebykey(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject returnObj = new JSONObject();
		String key=request.getParameter("key");
		String value=request.getParameter("value");
		Department department=new Department();
		department.setId(key);
		department.setName(value);
		List<Department> list = departmentService.getDepartment(department);
		returnObj.element("httpCode", "OK");//成功
		if(list!=null&&list.size()>0){
			returnObj.element("keyName", list.get(0).getName());//成功
		}else{
			returnObj.element("keyName", "");//成功
		}
		
		super.responsePrint(request, response, returnObj.toString());
	}
	
	/**
     * 根据userID数据字典获取部门名称
     * @param request
     * @param response
     * @param modelMap
     * @throws Exception 
     */
	@RequestMapping(value = "departmentByUserId", method = RequestMethod.GET)
	@ResponseBody
	public void queryValuebyUserkey(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject returnObj = new JSONObject();
		String key=request.getParameter("key");
		String value=request.getParameter("value");
		DepUser depUser = depUserService.getDepUserByUserID(key);
		Department department=new Department();
		department.setId(depUser.getDepId());
		department.setName(value);
		List<Department> list = departmentService.getDepartment(department);
		returnObj.element("httpCode", "OK");//成功
		if(list!=null&&list.size()>0){
			returnObj.element("keyName", list.get(0).getName());//成功
		}else{
			returnObj.element("keyName", "");//成功
		}
		
		super.responsePrint(request, response, returnObj.toString());
	}
	
	 /**
     * 根据数据字典获取下拉
     * @param request
     * @param response
     * @param modelMap
     * @throws Exception 
     */
	@RequestMapping(value = "queryLxsValuebykey", method = RequestMethod.GET)
	@ResponseBody
	public void queryLxsValuebykey(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject returnObj = new JSONObject();
		String key=request.getParameter("key");
		String value=request.getParameter("value");
		Department department=new Department();
//		department.setId(key);
//		department.setName(value);
		department.setTravelFlg("01");
		List<Department> list = departmentService.getDepartment(department);
		JSONArray jsonarray = JSONArray.fromObject(list);  
		responsePrint(request,response, jsonarray.toString());
		
		super.responsePrint(request, response, returnObj.toString());
	}
	
}


